﻿#ifndef TESTGRPCSERVER_H
#define TESTGRPCSERVER_H
#include "stdfax.h"
#include "IgRpcService.h"

#include <iostream>
#include <memory>
#include <string>
#include <thread>

#include "grpcpp/ext/proto_server_reflection_plugin.h"
#include "grpcpp/grpcpp.h"
#include "grpcpp/health_check_service_interface.h"
#include "grpc/support/log.h"
#include "grpc/grpc.h"
#include "grpcpp/security/server_credentials.h"
#include "grpcpp/impl/codegen/sync_stream.h"
#include "grpcpp/server.h"
#include "grpcpp/server_builder.h"
#include "grpcpp/server_context.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerReader;
using grpc::ServerReaderWriter;
using grpc::ServerWriter;
using grpc::Status;
using grpc::ServerAsyncResponseWriter;
using grpc::ServerCompletionQueue;


#include "helloworld.grpc.pb.h"

using helloworld::Greeter;
using helloworld::HelloReply;
using helloworld::HelloRequest;

// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
    Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override;
    //Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply);
};

class ServerImpl final {
 public:

  void Run();
  void HandleRpcs();
 public:
  std::unique_ptr<ServerCompletionQueue> cq_;
  Greeter::AsyncService service_;
  //std::unique_ptr<Server> server_;
 //public:
    //ServerImpl();
    //~ServerImpl();
 private:
  class CallData {
   public:
    CallData(Greeter::AsyncService* service, ServerCompletionQueue* cq)
        : service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) {

        // Invoke the serving logic right away.
        Proceed();
    }
    void Proceed();

   private:
    Greeter::AsyncService* service_;
    ServerCompletionQueue* cq_;
    ServerContext ctx_;
    HelloRequest request_;
    HelloReply reply_;
    ServerAsyncResponseWriter<HelloReply> responder_;
    enum CallStatus { CREATE, PROCESS, FINISH };
    CallStatus status_;  // The current serving state.
  };

};
//class TestGRPCServer : public IGRPCService
class TestGRPCServer : public IService
{
public:
    static TestGRPCServer * Instance();

    //gRPC同步调用,包括初始化、注册、启动
    virtual void OSGI_API TestgRPCSynServerRun(IGRPCService * m_pGRPCService,std::string server_address, bool enablepoll, int min_poll, int max_poll);
    /*服务的句柄和配置信息，须传递服务的IP和端口,插件名称*/

    //gRPC异步调用初始化
    virtual void OSGI_API TestgRPCAsynServerRun(IGRPCService * m_pGRPCService,std::string server_address);
    /*服务的句柄和配置信息，须传递服务的IP和端口,插件名称*/
    //服务插件带有cq和server,注册的插件类，测试插件

    //virtual int OSGI_API TestgRPCAsynServerRun(IGRPCService * m_pGRPCService,std::string server_address,
    //                                            std::unique_ptr<ServerCompletionQueue> cq_,
    //                                            std::unique_ptr<Server> server_,
    //                                            grpc::Service service_);   /*配置文件，须传递服务的IP和端口,插件名称*/

private:
    TestGRPCServer();
    ~TestGRPCServer();

private:
    static TestGRPCServer * m_pInstance;
    GreeterServiceImpl service;     //同步方法的类
    ServerImpl server;           //异步方法的类
};

#endif // TESTGRPCSERVER_H
